<template>
  <div>
    <dialog-edit :visible.sync="dialogVisible" :title="$attrs.title" ref="userManageEdit" width="1200"
                 height="500" :fullscreen="true" @save="save" :saveLoading="saveLoading">
      <template slot="body">
        <!--                <vxe-button @click="dialogVisible = false">返回</vxe-button>-->
        <dym-form :formData="lockFormData" :components="components" :formRules="formRules"
                  ref="dymForm">
<!--          <vxe-input prop="Parenttaskid" v-model="formData.Parenttaskid" :maxlength="30" :disabled="true" slot="Parenttaskid"></vxe-input>-->
        </dym-form>
      </template>
    </dialog-edit>
  </div>
</template>

<script>
import dialogEdit from '@/components/element/dialogEdit'
import dymForm from '@/components/element/dymForm'
import tipTap from "@/components/element/tipTap";
import projectPlanApi from "@/api/projectPlanApi";
export default {
  name: "PlanAdd",
  components: {
    dialogEdit,
    dymForm
  },
  props: {
    visible: {
      type: Boolean,
      default() {
        return false;
      }
    },
    formData: {
      type: Object,
      default() {
        return {};
      }
    }
  },
  computed: {
    dialogVisible: {
      get: function () {
        return this.visible
      },
      set: function (value) {
        this.$emit('update:visible', value)
      }
    }
  },
  data () {
    const defaultTime = ['09:00:00', '18:00:00']
    return {
      lockFormData: {},
      saveLoading: false,
      formRules: {
        TaskDate: [{required: true, message: '请选择开始结束时间'}]
      },
      components: [
        {
          label: '版本',
          span: 24,
          placeholder: '请输入版本',
          prop: 'Vesionno',
          value: 'Vesionno',
          selectTitle: '模块',
          componentName: 'vxe-input',
          attrs: {}
        },
        {
          label: '计划状态',
          span: 24,
          placeholder: '请选择计划状态',
          prop: 'Status',
          value: 'Status',
          componentName: 'SelectDiction',
          DictTypeCode: 'TASK_STATUS'
        },
        {
          label: '标题',
          span: 24,
          placeholder: '请输入标题',
          prop: 'Title',
          value: 'Title',
          componentName: 'vxe-input',
          attrs: {}
        },
        {
          label: '描述',
          span: 24,
          placeholder: '请输入描述',
          prop: 'Content',
          value: 'Content',
          componentName: tipTap,
          attrs: {
            width: '100%',
            height: '500px'
          }
        },
        {
          label: '预计开始时间和结束时间',
          span: 24,
          placeholder: '请选择',
          prop: 'TaskDate',
          value: 'TaskDate',
          componentName: 'el-date-picker',
          type: 'datetimerange',
          attrs: {
            'range-separator': 'TO',
            'start-placeholder': '开始日期',
            'end-placeholder': '结束日期',
            'default-time': defaultTime
          }
        },
        {
          label: '实际开始时间和结束时间',
          span: 24,
          placeholder: '请选择',
          prop: 'Actual',
          value: 'Actual',
          componentName: 'el-date-picker',
          type: 'datetimerange',
          attrs: {
            'range-separator': 'TO',
            'start-placeholder': '开始日期',
            'end-placeholder': '结束日期',
            'default-time': defaultTime
          }
        },
      ],
      Parentplanid: {
        label: '父计划',
        span: 24,
        placeholder: '请输入标题',
        prop: 'Parentplanid',
        value: 'Parentplanid',
        componentName: 'vxe-input',
        attrs: {}
      }
    }
  },
  methods: {
    save () {
      this.$refs.dymForm.$refs.form.validate().then((valid) => {
        if (this.lockFormData.TaskDate && this.lockFormData.TaskDate.length === 2) {
          if (typeof this.lockFormData.TaskDate[0] === "number") {
            this.$set(this.lockFormData, 'Started', this.lockFormData.TaskDate[0])
            this.$set(this.lockFormData, 'Ended', this.lockFormData.TaskDate[1])
          } else {
            this.$set(this.lockFormData, 'Started', this.lockFormData.TaskDate[0].getTime())
            this.$set(this.lockFormData, 'Ended', this.lockFormData.TaskDate[1].getTime())
          }
        }
        if (this.lockFormData.Actual && this.lockFormData.Actual.length === 2) {
          if (typeof this.lockFormData.Actual[0] === "number") {
            this.$set(this.lockFormData, 'Actualstarttime', this.lockFormData.Actual[0])
            this.$set(this.lockFormData, 'Actualendtime', this.lockFormData.Actual[1])
          } else {
            this.$set(this.lockFormData, 'Actualstarttime', this.lockFormData.Actual[0].getTime())
            this.$set(this.lockFormData, 'Actualendtime', this.lockFormData.Actual[1].getTime())
          }
        }
        if (this.lockFormData.Id) {
          this.editData()
        } else {
          this.createData()
        }
      }).catch((e) => {
        this.$XModal.message({message: `表单填写错误，请检查`, status: 'error'})
      })
    },
    createData () {
      let c = Object.assign(this.lockFormData, {
        Projectid: this.$route.params.id,
      })
       let a = JSON.parse(JSON.stringify(this.formRules))
      this.saveLoading = true
      projectPlanApi.create(c).then(res => {
        if (res) {
          if (res.code) {
            this.success(res.message)
            this.$emit('search')
            this.dialogVisible = false
          } else {
            this.warning(res.message)
          }
        }
      }).finally(() => {
        this.saveLoading = false
      })
    },
    editData () {
      this.saveLoading = true
      projectPlanApi.update(this.lockFormData).then(res => {
        if (res) {
          if (res.code) {
            this.success(res.message)
            this.$emit('search')
            this.dialogVisible = false
          } else {
            this.warning(res.message)
          }
        }
      }).finally(() => {
        this.saveLoading = false
      })
    },
    initData (row) {
      if (row.Status) {
        row.Status = row.Status.toString()
      }
      if (row.Started && row.Ended) {
        let s = row.Started.toString().length === 13 ? row.Started : row.Started * 1000
        let e = row.Ended.toString().length === 13 ? row.Ended : row.Ended * 1000
        row.TaskDate = [s, e]
      }
      if (row.Actualstarttime && row.Actualendtime) {
        let s = row.Actualstarttime.toString().length === 13 ? row.Actualstarttime : row.Actualstarttime * 1000
        let e = row.Actualendtime.toString().length === 13 ? row.Actualendtime : row.Actualendtime * 1000
        row.Actual = [s, e]
      }
    },
    getDataById () {
      projectPlanApi.getById(this.lockFormData).then(res => {
        if (res) {
          this.lockFormData = res
          this.initData(this.lockFormData)
        }
      })
    }
  },
  watch: {
    //
    dialogVisible: {
      handler (n, o) {
        if (n) {
          this.lockFormData = JSON.parse(JSON.stringify(this.formData))
          if (this.lockFormData.Id) {
            this.getDataById()
          }
          // if (this.formData.hasOwnProperty('Parentplanid') && this.formData.Parentplanid) {
          //   if (!this.components.some(it => it.prop === 'Parentplanid')) {
          //     this.components.splice(2, 0, this.Parentplanid)
          //   }
          // } else {
          //   if (this.components.some(it => it.prop === 'Parentplanid')) {
          //     this.components.splice(2, 1)
          //   }
          // }
        } else {
          this.lockFormData = {}
        }
      }
    }
  }
}
</script>

<style scoped>

</style>
